package com.bestom.bluetoothlock.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;

import com.bestom.bluetoothlock.LockApplication;
import com.bestom.bluetoothlock.R;
import com.bestom.bluetoothlock.activity.DeviceFunctionActivity;
import com.bestom.bluetoothlock.activity.MainActivity;
import com.bestom.bluetoothlock.activity.SearchGuidanceActivity;
import com.bestom.bluetoothlock.adapter.DeviceListAdapter;
import com.bestom.bluetoothlock.entity.DeviceData;
import com.bestom.bluetoothlock.entity.DeviceInfo;
import com.bestom.bluetoothlock.entity.EventDevice;
import com.bestom.bluetoothlock.entity.UserInfo;
import com.bestom.bluetoothlock.util.Constants;
import com.bestom.bluetoothlock.util.CurrencyDataManage;
import com.bestom.bluetoothlock.util.LogMsg;
import com.bestom.bluetoothlock.util.LogUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class DeviceFragment extends Fragment {
    private MainActivity activity;
    private DeviceListAdapter adapter;
    private Handler handler = new Handler() {
        public void handleMessage(Message message) {
            super.handleMessage(message);
            int i = message.what;
        }
    };
    private boolean isClick = false;
    public List<DeviceInfo> list = new ArrayList<>();
    @BindView(R.id.list_device)
    ListView list_device;
    @BindView(R.id.ll_no_bind_device)
    LinearLayout ll_no_bind_device;
    @BindView(R.id.tv_bind_device)
    TextView tv_bind_device;
    @BindView(R.id.tv_user_name)
    TextView tv_user_name;
    private View view;

    @Override
    @Nullable
    public View onCreateView(@NonNull LayoutInflater layoutInflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle) {
        this.activity = (MainActivity) getActivity();
        this.view = layoutInflater.inflate(R.layout.fragment_device, viewGroup, false);
        ButterKnife.bind((Object) this, this.view);
        EventBus.getDefault().register(this);
        initView();
        return this.view;
    }

    private void initView() {
        this.adapter = new DeviceListAdapter(getActivity());
        refreshLockList();
        this.list_device.setAdapter(this.adapter);
        this.list_device.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long j) {
                Intent intent = new Intent(DeviceFragment.this.getActivity(), DeviceFunctionActivity.class);
                intent.putExtra(Constants.KEY_VALUE, DeviceFragment.this.list.get(i));
                DeviceFragment.this.startActivity(intent);
            }
        });
    }

    private void refreshLockList() {
        int i = LockApplication.getSharedPreferencesHelper().getInt("count");
        LogUtils.d("refreshLockList","count --->" + i);
        this.list.clear();
        if (i > 0) {
            this.ll_no_bind_device.setVisibility(View.GONE);
            for (int i2 = 0; i2 < i; i2++) {
                DeviceInfo deviceInfo = new DeviceInfo();
                String mac = LockApplication.getSharedPreferencesHelper().getString("mac" + i2);
                deviceInfo.setLockmac(mac);
                String name = LockApplication.getSharedPreferencesHelper().getString("name_" + mac);
                if (name.isEmpty()) {
                    deviceInfo.setNickname("DLock");
                } else {
                    deviceInfo.setNickname(name);
                }
                this.list.add(deviceInfo);
            }
        } else {
            this.ll_no_bind_device.setVisibility(View.VISIBLE);
        }
        this.adapter.setData(this.list);
    }

    /* access modifiers changed from: private */
    public void setUpdata(DeviceData deviceData) {
        List<DeviceInfo> locklist = deviceData.getLocklist();
        this.list.clear();
        if (!(locklist == null || locklist.size() == 0)) {
            for (int i = 0; i < locklist.size(); i++) {
                DeviceInfo deviceInfo = locklist.get(i);
                deviceInfo.setLockmac(deviceInfo.getLockmac().toUpperCase());
                this.list.add(deviceInfo);
            }
        }
        this.adapter.setData(this.list);
    }

    @Subscribe
    public void onEventMainThread(EventDevice eventDevice) {
        if (eventDevice.getType() == 1) {
            LogMsg.Mess("刷新UI");
            refreshLockList();
        }
    }

    /* access modifiers changed from: package-private */
    @OnClick({R.id.iv_setting, R.id.tv_bind_device})
    public void onClickView(View view2) {
        int id = view2.getId();
        if (id == R.id.iv_setting || id == R.id.tv_bind_device) {
            startActivityForResult(new Intent(getActivity(), SearchGuidanceActivity.class), 1);
        }
    }

    public void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        if (i2 == 2) {
            LogMsg.Mess(intent.getExtras().getString(Constants.INTENT_EXTRA_KEY_QR_SCAN));
        }
    }

    public void onResume() {
        super.onResume();
        DeviceListAdapter deviceListAdapter = this.adapter;
        if (deviceListAdapter != null) {
            deviceListAdapter.notifyDataSetChanged();
        }
    }

    public void onPause() {
        super.onPause();
    }

    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }
}
